/**
 * Created by meiguizhinian on 2018/7/30
 * Copyright © 2018 lixiaolong. All rights reserved.
 */

//TODO：等待完成
//单例/全静态/枚举
ivy.GameCameraManager = {
	getInstance: function () {
		return this;
	},

	//初始化摄像机 须在场景初始化时调用
	//void init(cocos2d::Node* scene, cocos2d::CameraFlag cFlag = cocos2d::CameraFlag::USER1);
	setCameraInitEndFunc : function(func) {
		this.cameraInitEndFunc_ = func;
	},
	//摄像机管理器更新函数
	//void update();
	//管理器清理函数(一般在场景切换时调用)
	//void clear();
	getFoucsType : function() {
		return this.foucsType_;
	},
	//设置焦点
	setFoucs : function(pointOrObject, vecOffset=null) {
		if (pointOrObject instanceof ivy.BaseObject) {
			this.foucsObject_ = pointOrObject;
			this.foucsType_ = ivy.GameCameraManager.FoucsType.eBaseObject;
			this.cameraFoucsOffset_ = vecOffset || cc.p(0, 100);
		} else {
			this.foucsPoint_ = pointOrObject;
			this.foucsType_ = ivy.GameCameraManager.FoucsType.ePosition;
			this.cameraFoucsOffset_ = vecOffset || cc.p();
		}
	},
	setCameraFocusOffset : function(offset) {
		this.cameraFoucsOffset_ = offset;
	},
	getCameraFocusOffset : function() {
		return this.cameraFoucsOffset_;
	},
	getFocusObject : function() {
		return this.foucsObject_;
	},
	//设置跟随模式为立即更新
	setFollowImmediately : function() {
		this.followType_ = ivy.GameCameraManager.FollowType.eImmediately;
	},
	//设置跟随模式为变速跟随 par越大跟随速度越小
	setFollowVaryingVelocity : function(par) {
		this.followType_ = ivy.GameCameraManager.FollowType.eVaryingVelocity;
		this.varyingVelocityPar_ = par;
	},
	setFollowUniformlyVelocity : function(par) {
		this.followType_ = ivy.GameCameraManager.FollowType.eUniformlyVelocity;
		this.uniformlyVelcityPar_ = par;
	},
	setLimitZone : function(rect) {
		this.limitRect_ = rect;
	},
	getCameraPosition : function() {
		if (this.gameCamera_)
			return this.gameCamera_.getPosition();
		return cc.p();
	},
	getCameraOffset : function() {
		return cc.p(this.getCameraLeft(), this.getCameraTop());
	},
	getCameraLeft : function() {
		return this.getCameraPosition().x - ivy.GlobleConfig.getInstance().getScreenHalfWidth() / this.getCameraScale();
	},
	getCameraTop : function() {
		return this.getCameraPosition().y - ivy.GlobleConfig.getInstance().getScreenHalfHeight() / this.getCameraScale();
	},
	//const cocos2d::Rect& getCameraSceneRect();
	getCamera : function() {
		return this.gameCamera_;
	},
	//////////////////////////////////////////////////////////////////////////
	setCameraShake : function(x, y) {
		if (x !== 0 || y !== 0) {
			this.needShake_ = true;
		}
		this.shakeInXAxis_ = x;
		this.shakeInYAxis_ = y;
		this.shakeTimer_ = 0;
	},
	setCameraRandomShake : function(shakeTime, shakeV) {
		this.shakeInXYAxisByRandom_ = shakeV;
		this.shakeRandomMaxTimer_ = shakeTime;
	},
	//void doShakeLogic();
	//////////////////////////////////////////////////////////////////////////
	// void setCameraScaleTo(float sv, int32_t toDest, int32_t stopDest, int32_t toSrc);
	// void setCameraScaleTo(CameraScaleState state, int32_t time, float scale_v = 1.0f, bool saveDestScaleV = true);
	// void setCameraScaleTo(float scale_v);
	// void setCameraScaleLockTo(BaseObject* obj, CameraScaleState state, int32_t time, float scale_v = 1.0f);
	// void doCameraScaleLogic();
	getCameraScaleValue : function() {
		return this.curSceneNodeScaleV;
	},
	isInScaleHoldMode : function() {
		return this.isInScaleHoldMode_;
	},
	getCameraScale : function() {
		if (this.gameCamera_) {
			let standardV = ivy.GlobleConfig.getInstance().getScreenHeight() / 1.1566;
			return  standardV / this.gameCamera_.getPositionZ();
		}
		return 0.0;
	},
	//////////////////////////////////////////////////////////////////////////
	setCameraLock : function(type, lockRect=null) {
		this.cameraLockType_ = type;
		this.cameraLockRect_ = lockRect || cc.rect();
		let origin = ivy.Tools.ConvertPointToGL(cc.p(this.cameraLockRect_));
		this.cameraLockRect_.x = origin.x;
		this.cameraLockRect_.y = origin.y;
	},
	getCameraLockType : function() {
		return this.cameraLockType_;
	},
	getCameraLockRect() {
		return this.cameraLockRect_;
	},
	// void setCameraStaticLimit(CameraStaticLimitType type, int32_t v);
	// int32_t getCameraStaticLimit(CameraStaticLimitType type);
	setCameraScaleFunction : function(func) {
		this.cameraScaleFunction_ = func;
	},
	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////private:
	cameraStaticLimitLeft_ : -1,
	cameraStaticLimitRight_ : -1,
	cameraStaticLimitTop_ : -1,
	cameraStaticLimitBottom_ : -1,
	setCameraScale : function(scaleV) {
		if (this.gameCamera_) {
			let standardV = ivy.GlobleConfig.getInstance().getScreenHeight() / 1.1566;
			this.gameCamera_.setPositionZ(standardV/scaleV);
		}
	},
	cameraScaleFunction_ : null,//std::function<void()>
	//////////////////////////////////////////////////////////////////////////
	//摄像机实例
	gameCamera_ : null,//cocos2d::Camera*
	//当前焦点类型
	foucsType_ : 0,//FoucsType::eNone;
	//焦点对象实例
	foucsPoint_ : cc.p(),
	foucsObject_ : null,//cc::BaseObject*
	//跟随模式
	followType_ : 0,//FollowType::eNone;
	varyingVelocityPar_ : 10.0,
	uniformlyVelcityPar_ : 10.0,
	limitRect_ : cc.rect(),
	//震屏相关变量
	shakeInXAxis_ : 0,
	shakeInYAxis_ : 0,
	shakeTimer_ : 0,
	shakeInXYAxisByRandom_ : 0,
	shakeInXAxisByRandom_ : 0,
	shakeInYAxisByRandom_ : 0,
	shakeRandomMaxTimer_  : -1,
	needShake_ : false,
	//////////////////////////////////////////////////////////////////////////
	cameraLockType_ : 0,// CameraLockType::eLockNone;
	cameraLockRect_ : cc.rect(),//cocos2d::Rect
	//////////////////////////////////////////////////////////////////////////
	//缩放相关变量
	cssV_ : 0,// CameraScaleState::eScaleEnded;
	curSceneNodeScaleV : 1.0,
	cameraDestScaleV_ : 1.0,
	lastScaleV : 1.0,
	scaleToDestVTime : 0,
	scaleStopInDestVTime : 0,
	scaleToSrcVTime : 0,
	scaleTimer : 0,
	isInScaleHoldMode_ : false,
	//////////////////////////////////////////////////////////////////////////
	cameraFoucsOffset_ : cc.p(),
	cameraInitEndFunc_ : null,//std::function<void(void)>
	//////////////////////////////////////////////////////////////////////////
	sceneCameraRect_ : cc.rect(),//cocos2d::Rect
	needRefreshSceneCameraRect_ : true,
	//////////////////////////////////////////////////////////////////////////
	lockScaleToObject_ : null,//BaseObject*
	saveScaleValue_ : 1.0,
};

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ivy.GameCameraManager.CameraStaticLimitType = {
	eLeft   : 0,
	eRight  : 1,
	eTop    : 2,
	eBottom : 3,
};
ivy.GameCameraManager.CameraLockType = {
	eLockNone       : 0,
	eLockLeftRight  : 1,
	eLockUpBottom   : 2,
	eLockAll        : 3,
};

ivy.GameCameraManager.CameraScaleState = {
	eScaleStart     : 0,
	eScaleHold      : 1,
	eScaleEndingq   : 2,
	eScaleEnded     : 3,
};

ivy.GameCameraManager.FoucsType = {
	//初始无效状态
	eNone       : 0,
	//焦点为对象
	eBaseObject : 1,
	//焦点为位置
	ePosition   : 2,
};

ivy.GameCameraManager.FollowType = {
	eNone               : 0,
	//立即更新
	eImmediately        : 1,
	//变速跟随
	eVaryingVelocity    : 2,
	//匀速跟隨
	eUniformlyVelocity  : 3,
};